package com.huoyun.car.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.huoyun.base.action.BaseAction;
import com.huoyun.base.bean.Page;
import com.huoyun.car.bean.CarPubInfoCar;
import com.huoyun.car.model.BidCarInfoModel;
import com.huoyun.car.model.LoadAmountComparator;
import com.huoyun.car.model.SelectedCarsModel;
import com.huoyun.car.service.CarPubInfoService;
import com.huoyun.car.service.CarRouteService;
import com.huoyun.car.service.CarService;
import com.huoyun.car.service.evt.QueryCarForGoodsEvt;
import com.huoyun.cargo.bean.CargoInfo;
import com.huoyun.cargo.service.CargoService;
import com.huoyun.common.service.BidInfoService;

public class ChooseCarAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 默认匹配的可超载吨位数
	 */
	private static final int UP_WEIGHT_NUM = 3;
	
	/**
	 * 默认匹配的运载吨位数差
	 */
	private static final int DOWN_WEIGHT_NUM = 2;
	
	/**
	 * 默认匹配的可超载体积数
	 */
	private static final int UP_VOLUME_NUM = 10;
	
	/**
	 * 默认匹配的运载体积数差
	 */
	private static final int DOWN_VOLUME_NUM = 10;
	
	
	private CarPubInfoService carPubInfoService;
	private CarService carService;
	
	private CarRouteService carRouteService;
	
	private BidInfoService bidInfoService;
	private CargoService cargoService;
	
	// 想要匹配车源的货源信息
	private CargoInfo cargo;
	
	private Page selectedCarPage;
	
	/**
	 * 货物id
	 */
	private long goodsId;
	
	/**
	 * 订单涉及的车辆id集合,用“,”分开
	 */
	private String selectedCarIds;
	
	/**
	 * 系统推荐的方案集合
	 */
	private List<SelectedCarsModel> selectedCarList;
	
	/**
	 * 用户选择的拼车方案
	 */
	private SelectedCarsModel selectedCars;
	
	/**
	 * 车源信息id
	 */
	private long carPubId;
	
	private List<CarPubInfoCar> searchResult;
	
	private List<BidCarInfoModel> bidCarInfos;

	/**
	 * 根据条件查找车源
	 * 
	 * 给货物找车
	 * 
	 * @return 返回操作结果
	 */
	public String queryCars() {
		try 
		{
			CargoInfo cargoInfo=cargoService.getCargoInfo(getLoginUser().getId(),goodsId);
			
			//货物不存在或者当前用户没有操作权限
			if(cargoInfo == null)
			{
				return INPUT;
			}
			
			QueryCarForGoodsEvt queryCarForGoodsEvt = new QueryCarForGoodsEvt();
			queryCarForGoodsEvt.setStartProvince(cargoInfo.getStartProvince());
			queryCarForGoodsEvt.setStartCity(cargoInfo.getStartCity());
			queryCarForGoodsEvt.setEndProvince(cargoInfo.getDestinationProvince());
			queryCarForGoodsEvt.setEndCity(cargoInfo.getDestinationCity());
			queryCarForGoodsEvt.setGoodsId(goodsId);
			queryCarForGoodsEvt.setBidEndTime(cargoInfo.getBidEndTime());
			
			//按照当前货物属性查询相符合的车辆信息
			selectedCarPage = carPubInfoService.queryCarForGoods(queryCarForGoodsEvt);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return ERROR;
		}
		
		return SUCCESS;
	}

	/**
	 * 系统推荐车辆方案
	 * @return
	 */
	public String commendCar() 
	{
		CargoInfo cargoInfo=cargoService.getCargoInfo(getLoginUser().getId(),goodsId);
		
		//货物不存在或者当前用户没有操作权限
		if(cargoInfo == null)
		{
			return INPUT;
		}
		
		//所有应标车辆
		bidCarInfos = carPubInfoService.queryBidCars(goodsId, null);
		
		//按照车辆的可载量（吨位或者体积）从大到小排序
		Collections.sort(bidCarInfos, new LoadAmountComparator());
		
		selectedCarList = new ArrayList<SelectedCarsModel>();
		
		//推荐单车方案
		if(bidCarInfos != null && bidCarInfos.size() > 0)
		{			
			Float maxLoadAmount = Float.parseFloat(bidCarInfos.get(0).getLoadAmount());
			
			Float cargoodsAmount = Float.parseFloat(cargoInfo.getAmount());
			//推荐单车方案
			if(maxLoadAmount.compareTo(cargoodsAmount - DOWN_WEIGHT_NUM) >= 0)
			{
				for(BidCarInfoModel bidCarInfo : bidCarInfos)
				{
					Float loadAmount = Float.parseFloat(bidCarInfo.getLoadAmount());
					
					if(loadAmount.compareTo(cargoodsAmount - DOWN_WEIGHT_NUM) >= 0 
							&& loadAmount.compareTo(cargoodsAmount + UP_WEIGHT_NUM) <= 0)
					{
						SelectedCarsModel selectedCarsModel = new SelectedCarsModel();
						
						List<BidCarInfoModel> BidCarInfoList = new ArrayList<BidCarInfoModel>();
						BidCarInfoList.add(bidCarInfo);
						
						selectedCarsModel.setBidCarInfoModels(BidCarInfoList);
						selectedCarsModel.setCarIds(bidCarInfo.getCarId() + "");
						selectedCarsModel.setGoodsId(goodsId);
						
						selectedCarList.add(selectedCarsModel);
					}
				}
			}			
		}
		
		//推荐双车方案
		if(bidCarInfos != null && bidCarInfos.size() > 1)
		{
			Float maxLoadAmount = Float.parseFloat(bidCarInfos.get(0).getLoadAmount()) + Float.parseFloat(bidCarInfos.get(1).getLoadAmount());
			
			Float cargoodsAmount = Float.parseFloat(cargoInfo.getAmount());
			
			if(maxLoadAmount.compareTo(cargoodsAmount - DOWN_WEIGHT_NUM) >= 0)
			{
				for(int i = 0; i < bidCarInfos.size(); i++)
				{
					for(int j = i; j < bidCarInfos.size(); j++)
					{
						Float loadAmount = Float.parseFloat(bidCarInfos.get(i).getLoadAmount()) + Float.parseFloat(bidCarInfos.get(j).getLoadAmount());
						
						if(loadAmount.compareTo(cargoodsAmount - DOWN_WEIGHT_NUM) >= 0 
								&& loadAmount.compareTo(cargoodsAmount + UP_WEIGHT_NUM) <= 0)
						{
							SelectedCarsModel selectedCarsModel = new SelectedCarsModel();
							
							List<BidCarInfoModel> BidCarInfoList = new ArrayList<BidCarInfoModel>();
							BidCarInfoList.add(bidCarInfos.get(i));
							BidCarInfoList.add(bidCarInfos.get(j));
							
							selectedCarsModel.setBidCarInfoModels(BidCarInfoList);
						
							selectedCarsModel.setCarIds(bidCarInfos.get(i).getCarId() + "," + bidCarInfos.get(j).getCarId());
							
							selectedCarsModel.setGoodsId(goodsId);
							
							selectedCarList.add(selectedCarsModel);
							
						}
					}
				}
			}			
		}
		
		
		return SUCCESS;
	}
	
	/**
	 * 自选车辆方案
	 * 
	 * @author 刘晓宁
	 * @return
	 */
	public String chooseCar()
	{
		String[] carIds = initSelectedCarIds(selectedCarIds);
		
		List<BidCarInfoModel> bidCarList = carPubInfoService.queryBidCars(goodsId, carIds);
		
		selectedCars = new SelectedCarsModel();
		selectedCars.setBidCarInfoModels(bidCarList);
		selectedCars.setCarIds(selectedCarIds);
		selectedCars.setGoodsId(goodsId);
		
		return SUCCESS;
	}
	
	/**
	 * 查询所有应标的车源信息，展示在页面供用户选择
	 * @author 刘晓宁
	 * @return 
	 */
	public String queryBidCars()
	{
		try
		{
			bidCarInfos = carPubInfoService.queryBidCars(goodsId, null);
		}
		catch (Exception e) 
		{
			e.fillInStackTrace();
			
			return ERROR;
		}
		return SUCCESS;
	}

	public long getCarPubId() {
		return carPubId;
	}

	public void setCarPubId(long carPubId) {
		this.carPubId = carPubId;
	}
	
	public long getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(long goodsId) {
		this.goodsId = goodsId;
	}
	public CarPubInfoService getCarPubInfoService() {
		return carPubInfoService;
	}

	public void setCarPubInfoService(final CarPubInfoService carPubInfoService) {
		this.carPubInfoService = carPubInfoService;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(final CarService carService) {
		this.carService = carService;
	}

	
	public CarRouteService getCarRouteService() {
		return carRouteService;
	}

	public void setCarRouteService(CarRouteService carRouteService) {
		this.carRouteService = carRouteService;
	}

	public CargoService getCargoService() {
		return cargoService;
	}

	public void setCargoService(CargoService cargoService) {
		this.cargoService = cargoService;
	}

	public BidInfoService getBidInfoService() {
		return bidInfoService;
	}

	public void setBidInfoService(final BidInfoService bidInfoService) {
		this.bidInfoService = bidInfoService;
	}

	public CargoInfo getCargo() {
		return cargo;
	}

	public void setCargo(final CargoInfo cargo) {
		this.cargo = cargo;
	}
	
	public List<CarPubInfoCar> getSearchResult() {
		return searchResult;
	}

	public void setSearchResult(List<CarPubInfoCar> searchResult) {
		this.searchResult = searchResult;
	}

	public Page getSelectedCarPage() {
		return selectedCarPage;
	}

	public void setSelectedCarPage(Page selectedCarPage) {
		this.selectedCarPage = selectedCarPage;
	}

	public List<BidCarInfoModel> getBidCarInfos() {
		return bidCarInfos;
	}

	public void setBidCarInfos(List<BidCarInfoModel> bidCarInfos) {
		this.bidCarInfos = bidCarInfos;
	}

	public String getSelectedCarIds() {
		return selectedCarIds;
	}

	public void setSelectedCarIds(String selectedCarIds) {
		this.selectedCarIds = selectedCarIds;
	}
	
	public String[] initSelectedCarIds(String selectedCarIds) {
		if(StringUtils.isNotEmpty(selectedCarIds))
		{
			return selectedCarIds.split(",");
		}

		return null;
	}

	public List<SelectedCarsModel> getSelectedCarList() {
		return selectedCarList;
	}

	public void setSelectedCarList(List<SelectedCarsModel> selectedCarList) {
		this.selectedCarList = selectedCarList;
	}

	public SelectedCarsModel getSelectedCars() {
		return selectedCars;
	}

	public void setSelectedCars(SelectedCarsModel selectedCars) {
		this.selectedCars = selectedCars;
	}
	
}
